package com.tarena.lbs.coupon.dao.repository.impl;

import com.alibaba.spring.util.ObjectUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.coupon.dao.mapper.CouponMapper;
import com.tarena.lbs.coupon.dao.repository.CouponRepository;
import com.tarena.lbs.coupon.pojo.po.CouponPO;
import com.tarena.lbs.coupon.pojo.query.CouponQuery;
import io.micrometer.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ===============================================
 * class: CouponRepositoryImpl
 * author: HanChao
 * date: 2025/9/1 10:30
 * DESCRIPTION: TODO (填写类的作用)
 * ===============================================
 */
@Repository
@Slf4j
public class CouponRepositoryImpl implements CouponRepository {
    @Autowired
    private CouponMapper couponMapper;

    /*
    * 查看详情
    * */
    @Override
    public CouponPO getCouponById(Integer id) {
        return couponMapper.selectById(id);
    }

    /*
    * 查询列表
    * */
    @Override
    public PageInfo<CouponPO> pageList(CouponQuery query) {
        QueryWrapper<CouponPO> queryWrapper = assembleAdminQuery(query);
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<CouponPO> pos = couponMapper.selectList(queryWrapper);
        return new PageInfo<>(pos);
    }

    @Override
    public void delete(Integer id) {
        couponMapper.deleteById(id);
        log.info("删除优惠券成功");
    }

    /*
    * 修改优惠券启用状态
    * */
    @Override
    public void updateStatus(Integer id, Integer enableStatus) {
        CouponPO couponPO=new CouponPO();
        couponPO.setEnableStatus(enableStatus);
        couponMapper.update(couponPO,new QueryWrapper<CouponPO>().eq("id",id));
        log.info("修改优惠券启用状态成功");
    }

    /*
    * 修改优惠券
    * */
    @Override
    public void updateCoupon(CouponPO couponPO) {
        couponMapper.updateById(couponPO);
        log.info("修改优惠券成功");
    }

    /*
    * 新增优惠券
    * */
    @Override
    public void saveCoupon(CouponPO couponPO) {
        couponMapper.insert(couponPO);
        log.info("新增优惠券成功");
    }

    /*
    * 优惠券定时任务查询
    * */
    @Override
    public List<CouponPO> findAll() {
        return couponMapper.selectList(new QueryWrapper<>());
    }
    /*
     * 优惠券定时任务查询(每5分钟)
     * */
    @Override
    public List<CouponPO> findNeedStatusUpdateCoupons(Date date, Date now) {
        Date fiveMinutesLater = DateUtils.addMinutes(now, 5);

        QueryWrapper<CouponPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(wrapper -> wrapper
                .or(wq -> wq
                        .eq("status", 0)
                        .between("start_date", now, fiveMinutesLater)
                )
                // 第二个条件组：进行中但即将结束
                .or(wq -> wq
                        .eq("status", 1)
                        .between("end_date", now, fiveMinutesLater)
                )
        );
        List<CouponPO> needCheckCoupons = couponMapper.selectList(queryWrapper);
        return needCheckCoupons;
    }

    /*
    * 查询列表中的查询条件
    * */
    private QueryWrapper<CouponPO> assembleAdminQuery(CouponQuery query) {
        QueryWrapper<CouponPO> queryWrapper = new QueryWrapper<>();
        //1.couponName 优惠券名称 模糊查询 非空 where coupon_name like(%#{}%)
        if (StringUtils.isNotBlank(query.getCouponName())){
            queryWrapper.like("coupon_name",query.getCouponName());
        }
        //2.couponType 优惠券类型 模糊查询 非空大于零
        if (query.getCouponType() != null){
            queryWrapper.eq("coupon_type",query.getCouponType());
        }
        //3.status 优惠券状态 模糊查询 非空大于零
        if (query.getStatus() != null){
            queryWrapper.eq("status",query.getStatus());
        }
        return queryWrapper;
    }
}


